package com.ppmall.service.impl;

import com.ppmall.dao.AdvertMapper;
import com.ppmall.pojo.Advert;
import com.ppmall.redis.RedisUtil;
import com.ppmall.service.IAdvertService;
import com.ppmall.util.JsonUtil;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

@Service
public class AdvertServiceImpl implements IAdvertService {


    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    AdvertMapper advertMapper;

    public List<Advert> getAllAdverts(){
        List<Advert> adverts;
        if(redisUtil.hasKey("allAdvert")) {
            adverts = (List<Advert>) redisUtil.get("allAdvert");
        } else {
            adverts = advertMapper.getAllAdverts();
            redisUtil.set("allAdvert", adverts);
        }

        return adverts;
    }

    public Advert getAdvertById(Integer id) {
        return advertMapper.getAdvertById(id);
    }

    public Integer updateAdvertById(Advert advert) {
        return advertMapper.updateAdvertById(advert);
    }

    public Integer deleteAdvertById(Integer id) {
        return advertMapper.deleteAdvertById(id);
    }

    public Integer addAdvert(Advert advert) {
        return advertMapper.addAdvert(advert);
    }

    public Integer deleteAllAdverts() {
        return advertMapper.deleteAllAdverts();
    }
}